<?php
/**
 * @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
 * @license https://opensource.org/licenses/GPL-2.0
 * @link https://www.koogua.com
 */

namespace App\Http\Home\Controllers;

use App\Http\Admin\Services\Agency;
use App\Http\Home\Controllers\Controller;
use App\Http\Home\Services\FullH5Url as FullH5UrlService;
use App\Repos\Course;
use App\Repos\CourseAgencies;
use App\Services\Logic\Teacher\TeacherList as TeacherListService;

/**
 * @RoutePrefix("/home/agency")
 */
class AgencyController extends Controller
{
    /**
     * @Get("/index", name="home.agency.index")
     */
    public function indexAction()
    {
        $service = new Agency();
        $pager = $service->getAgencies();
        $this->seo->prependTitle('机构');
        return $this->view->setVar('pager', $pager);
    }

    /**
     * @Get("/pager", name="home.agency.pager")
     */
    public function pagerAction()
    {
        $service = new Agency();
        $pager = $service->getAgencies();
        $pager->target = 'agency-list';
        $this->view->setVar('pager', $pager);
    }


    /**
     * @Get("/show/{id:[0-9]+}", name="home.agency.show")
     */
    public function showAction($id)
    {

        $service = new Agency();
        $item = $service->getAgency($id);
        $courseIds = [];
        $courseAgencyObj = new CourseAgencies();
        $results = $courseAgencyObj->findByAgencyId([$id]);
        foreach ($results as $v) {
            array_push($courseIds, $v->course_id);
        }

        $agencyCourseObj = new Course();
        $courseList = $agencyCourseObj->findByIds($courseIds);
        $results = [];
        foreach ($courseList as $k => $v) {
            if ($v->published == 0) {
                continue;
            }
            $results[] = $v;
        }
        $this->view->setVar('courseList', $results);
        $this->view->setVar('agency', $item);
    }
}